package ruifeng.zcj.dig.ui.shopping;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Base;
import ruifeng.zcj.dig.ui.base.RefreshRecyclerFragment;
import ruifeng.zcj.mybase.base.App;
import ruifeng.zcj.mybase.base.utils2.CacheManager;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 公共的分类
 * Created by Administrator on 2016/9/27 0027.
 */
public abstract class CategoryFragment extends RefreshRecyclerFragment {

    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }

    /****
     * 网络请求数据具体的子类实现
     **/
    protected void sendRequestData() {
        mRxManager.add(
                Observable.create(new Observable.OnSubscribe<Base>() {
                    @Override
                    public void call(final Subscriber<? super Base> subscriber) {
                        if (!subscriber.isUnsubscribed()) {
                            OSChinaApi.getsuppliergoodstypelist(categoryType(), supplierid(), new RequestCallBack<String>() {
                                @Override
                                public void onSuccess(ResponseInfo<String> responseInfo) {
                                    if (!TextUtils.isEmpty(responseInfo.result)) {
                                        showSnackBar("请求成功");
                                        sendRequestDataSucess(responseInfo.result);
                                        if (isAdded()) {
                                            if (mState == STATE_REFRESH) {
                                                onRefreshNetworkSuccess();
                                            }
                                            Base base = fromJson(responseInfo.result);
                                            // 保存数据
                                            CacheManager.saveObject(App.getAppContext(), base, getCacheKey());
                                            subscriber.onNext(base);
                                            subscriber.onCompleted();//访问结束
                                        }
                                    }
                                }

                                @Override
                                public void onFailure(HttpException e, String s) {
                                    subscriber.onError(e);
                                    subscriber.onCompleted();//访问结束
                                }
                            });
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Base>() {
                    @Override
                    public void onCompleted() {
                        showSnackBar("获取成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAdded()) {
                            readCacheData(getCacheKey());
                        }
                        showSnackBar("服务器没有数据");
                    }

                    @Override
                    public void onNext(Base category) {
                        executeOnLoadDataSuccess(category);
                    }
                }));
    }

    /***
     * 获取分类的类型
     **/
    protected abstract int categoryType();

    protected String supplierid() {
        return null;
    }

    /**
     * 解析json
     */
    protected abstract Base fromJson(String netResult);

    /***
     * 不显示加载更多
     **/
    @Override
    protected boolean OnLoadingMore() {
        return false;
    }


    @Override
    protected long getAutoRefreshTime() {
        // 最新商品分类两小时刷新一次
        return 2 * 60 * 60;
    }


    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
    }


    @Override
    protected void onUserVisible() {
        System.out.println("郑朝军fldjlfjdlfjdklf");
    }

    @Override
    protected void onUserInvisible() {
        super.onUserInvisible();
    }

    @Override
    protected void cleanMemory() {
        super.cleanMemory();
       /* if (mRxManager != null){
            mRxManager.clear();
        }*/
    }


}
